/* eslint-disable no-unused-vars */
import namespace from "../../namespace";

/**
 * 画布 鼠标拖拽行为, 分发给所选图形 或 图形组(待实现)
 */

class DragDropPolicy extends namespace.policy.canvas.UniquePolicy {
  name = "namespace.policy.DragDropPolicy";
  constructor() {
    super();

    this.isDrag = false;
    this.mouseDownElement = null;
  }

  onMouseDown(canvas, x, y) {
    this.isDrag = false;
    let figure = canvas.getBestFigure(x, y);

    if (figure !== null && figure.isDraggable()) {
      figure.onDragStart(x - figure.getAbsoluteX(), y - figure.getAbsoluteY());
    }

    this.mouseDownElement = figure;
  }

  onMouseDrag(canvas, dx, dy, dx2, dy2, event) {
    this.isDrag = true;

    if (this.mouseDownElement) {
      let figures = canvas.getSelection();

      if (!figures.contains(this.mouseDownElement)) {
        this.mouseDownElement.onDrag(dx, dy, dx2, dy2, event);
      } else {
        figures.each((i, figure) => figure.onDrag(dx, dy, dx2, dy2, event));
      }
    }
  }

  onMouseUp(canvas, x, y) {
    let sel = canvas.getSelection().getAll();

    if (!sel.contains(this.mouseDownElement)) {
      this.mouseDownElement.onDragEnd(x, y);
    } else {
      sel.each((i, figure) => figure.onDragEnd(x, y));
    }

    this.mouseDownElement = null;
    this.isDrag = false;
  }
}

namespace.policy.canvas.DragDropPolicy = DragDropPolicy;
